/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils
{
	internal class GaussianImp implements IGaussian
	{
		private static const _2pi:Number = Math.PI;
		private static const _hwhm:Number = 2.35 / 2;
		private var _half:Number = _hwhm;
		private var _peak:Number = 0;
		public function GaussianImp(expectation:Number,hwhm:Number)
		{
			_half = hwhm / _hwhm;
			_peak = expectation;
		}
		private function normalDistribution():Number {
			var u1:Number = Math.random();
			var u2:Number = Math.random();

			return _half * Math.sqrt(-2 * Math.log(u1)) * Math.cos(_2pi * u2);
		}
		
		public function get float():Number {
			return normalDistribution() + _peak;
		}
		
		public function get integer():int {
			var g:Number = normalDistribution();
			return int(g+((g > 0)? .5 : -.5))+_peak;
		}
		
		public function get floatPositive():Number {
			var num:Number = float;
			return num < 0? -num : num;
		}
		
		public function get floatNegative():Number {
			var num:Number = float;
			return num < 0? num : -num;
		}
		
		public function get integerPositive():int {
			var i:int = integer;
			return i < 0? -i : i;
		}
		
		public function get integerNegative():int {
			var i:int = integer;
			return i < 0? i : -i;
		}
	}
}